//专门负责路由
var selectNum
var registrant
var Id

var pos
angular.module('salary',['ngRoute', 'NetworkRequest'])
    .config(['$routeProvider','$locationProvider',  function ($routeProvider, $locationProvider) {
        $locationProvider.hashPrefix("");
        $routeProvider
            .when('/addSalary',{
                // template:"登记",
                //相对于index.html
                // templateUrl: "./public/addEmployee.html"
                templateUrl: "/views/addSalary"

            })
            .when('/selectSalary', {
                // template:"查询",
                templateUrl: "/views/selectSalary"

            })
            .when('/resultSalary', {
                // template:"结果",
                templateUrl: "/views/resultSalary"

            }).when('/updateSalary', {
            // template:"结果",
            templateUrl: "/views/updateSalary"

            }).otherwise({
                    redirectTo:'/views/addSalary'
            })
    }])

    //薪酬登记
    .controller('addSalaryCtrl', ['$scope', '$http', function ($scope, $http){
        //获取创建时间
        $scope.creatTime = new Date().getTime()
        //获取登记人
        $scope.register = document.getElementById('sysName').innerHTML
        register = $scope.register
        //获取登记人的Id
        $scope.Id = document.getElementById('ID').innerHTML
        Id = $scope.Id
        //获取登记人的职位
        $scope.pos = document.getElementById('sysPos').innerHTML
        pos =  $scope.pos
        console.log($scope.register, $scope.Id )

        //基本工资发生变化
        $scope.baseWageChange = function () {
           // setTimeout(function () {
               console.log($scope.baseWage)
               $scope.transportMoney = parseInt($scope.baseWage) * 0.08
               $scope.lunchMoney = parseInt($scope.baseWage) * 0.02 + 3
               $scope.phoneMoney = parseInt($scope.baseWage) * 0.005
               $scope.total = parseInt($scope.baseWage) + $scope.transportMoney + $scope.lunchMoney +  $scope.phoneMoney
           // },1500)
        }

        //清空按钮
        $scope.clean = function () {
            console.log("清理")
            $scope.name = '',
            $scope.total = '',
                $scope.registrant = '',
                $scope.baseWage = ''
        }

        //提交按钮
        $scope.submit = function () {
            var creatTime =  document.getElementById('creatTime').value
            //获取登记人
            // var register = document.getElementById('sysName').innerHTML
            console.log($scope.name)
            console.log($scope.baseWage)
            console.log($scope.registrant)
            // console.log(register)
            console.log(creatTime)
            console.log($scope.total)

        if($scope.name == undefined){
                alert("薪酬标准名称不能为空！")
            return
        }else if($scope.baseWage == undefined){
            alert("基本工资不能为空！")
            return
        }else if($scope.registrant == undefined){
            alert("制定人不能为空！")
            return
        }




// return
            $scope.addSalary(
                $scope.name,
                $scope.baseWage,
                $scope.registrant,
                $scope.creatTime,//制定人
                $scope.register,//登记人
                $scope.total,


                function (res) {
                    alert("薪酬登记成功")
                    window.location.hash = "#/selectSalary"
                    console.log(res)
                },
                function (err) {
                    console.log(err)
                }
            )


        }


    }])

    //薪酬查询
    .controller('selectSalaryCtrl', ['$scope', '$http', function ($scope, $http){

        //清空按钮
        $scope.clean = function () {
            console.log("清理")
            $scope.name = '',
                $scope.registrant = '',
                $scope.baseWage = ''

        }


        //查询按钮点击
        $scope.selectSubmit = function () {

            // 获取薪酬标准编号、关键字、输入时间
            console.log($scope.id);
            console.log($scope.name);
            console.log($scope.creatTime);


            window.location.hash = "#/resultSalary"
        }
    }])
    //薪酬查询结果
    .controller('resultSalaryCtrl', ['$scope', '$http', function ($scope, $http){
        //薪酬列表
        $scope.selectSalary(function (res) {
            console.log(res)

            for(var i= 0 ; i <= res.result.length -1; i++){
                // console.log('12',res.data.isCheck)
                //    console.log(res.data.length)
                if( res.result[i].isCheck == 0){
                    res.result[i].isCheck = '未复核'
                }else{
                    res.result[i].isCheck = '已复核'
                }

            }

            for (var j = res.result.length -1; j>=0; j--) {
                console.log("123")
                if (res.result[j].isDelete == 1) {
                    // console.log('已经删除')
                    res.result.splice(j, 1);
                }
            }

            $scope.salarys = res.result

        },function (err) {
            console.log(err);
        })


        $scope.black = function () {
            window.location.hash = "#/selectSalary"

        }
        //选中薪酬ID
        $scope.checkSalary = function (id) {
            console.log(id)
            salaryID = id
            console.log(`薪酬ID:${salaryID}`);

            var con;
            con=confirm(`你确定要查看ID为${id}的薪酬表吗`); //在页面上弹出对话框
            if(con==true){

                console.log(`薪酬ID:${salaryID}`);
                window.location.hash = "#/updateSalary?id=" + id

            }
            else{
                return
            }

        }
    }])
    //薪酬修改
    .controller('updateSalaryCtrl', ['$scope', '$http', function ($scope, $http){

        // console.log(`薪酬ID:${$scope.salaryID}`);
        console.log(`薪酬ID:${salaryID}`);

        console.log(salaryID, Id, pos)


        // var fileNum = location.hash.las

        $scope.selectSalaryID(salaryID, function (res) {

            $scope.salarys = res
            console.log($scope.salarys[0] )
            $scope.id = $scope.salarys[0].id
            $scope.name = $scope.salarys[0].name
            $scope.total = $scope.salarys[0].total
            $scope.registrant = $scope.salarys[0].registrant
            $scope.register = $scope.salarys[0].register
            $scope.creatTime = $scope.salarys[0].creatTime
        },function (err) {
            console.log(err)
        })

        //修改姓名
        $scope.nameChange = function () {
            // alert('修改')
            setTimeout(function () {
                var employeeName =document.getElementById('nameChange').innerHTML
                $scope.name = employeeName
                console.log( $scope.name)
            },1000)
        }


        //修改年龄
        $scope.ageChange = function () {
            // alert('修改')
            setTimeout(function () {
                var age =document.getElementById('ageChange').innerHTML
                $scope.age = age
                console.log( $scope.age)
            },1000)
        }

        //修改档案
        $scope.updateBtn = function () {
            console.log('修改薪酬标准');
            if($scope.pos !== '薪酬经理'){
                alert('抱歉，你没有复核的权限！')
                return
            }
            var con;
            con=confirm(`你确定修改${salaryID}的文档吗`); //在页面上弹出对话框
            if(con==true){
                //查找档案
                console.log('修改档案',salaryID);

                $scope.updateEmployee(salaryID, $scope.name, $scope.age, function (res) {
                    console.log(res)
                    alert('修改档案成功')
                    window.location.hash = "#/resultEmployee"
                },function (err) {
                    cnosole.log(err)
                })
            }
            else{
                return
            }
        }
        //复核薪酬标准
        $scope.checkBtn = function () {
            console.log('复核薪酬标准');
            if($scope.pos !== '薪酬经理'){
                alert('抱歉，你没有复核的权限！')
                return
            }
            var con;
            con=confirm(`你确定复核${salaryID}的文档吗`); //在页面上弹出对话框
            if(con==true){
                //复核薪酬
                $scope.checkSalary(salaryID, function (res) {
                    console.log(res)
                    alert('薪酬标准复核成功')
                    window.location.hash = "#/resultSalary"
                },function (err) {
                    cnosole.log(err)
                })
            }
            else{
                return
            }
        }

        //删除档案
        $scope.deleteBtn  = function () {
            console.log('删除薪酬标准')
            if($scope.pos !== '薪酬经理'){
                alert('抱歉，你没有删除的权限！')
                return
            }
            var con;
            con=confirm(`你确定要删除ID为${salaryID}的薪酬标准吗`); //在页面上弹出对话框
            if(con==true){
                //查找档案


                $scope.deleteSalary(salaryID, function (res) {
                    console.log(res)
                    alert('删除薪酬标准成功')
                    window.location.hash = "#/resultSalary"
                },function (err) {
                    cnosole.log(err)
                })
            }
            else{
                return
            }
        }

        //返回薪酬标准查询
        $scope.backBtn  = function () {
            window.location.hash = "#/resultSalary"
        }





    }])